
<template>
  <div class="content_index">
    <div class="ci_header">
      <div class="ci_left sec-title">
        <el-icon
          style="vertical-align: middle; display: unset; cursor: pointer"
          @click="backList()"
          ><Back
        /></el-icon>
        <!-- <span>{{ route.name }}</span> -->
        <span>凭证申请</span>
      </div>
    </div>
    <div class="ci_content" style="margin: 12px 16px">
      <el-form
        :model="form"
        ref="VoucherFormRef"
        :rules="VoucherFormRefRules"
        label-width="120px"
        style="width: 100%"
      >
        <el-form-item label="DID：" prop="credentialSubject.id">
          <el-select
            v-model="form.credentialSubject.id"
            placeholder="请选择DID"
            style="width: 100%"
          >
            <el-option
              v-for="item of enumList.didList"
              :label="item.did"
              :value="item.did"
              :key="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="发证方：" prop="issuerId">
          <el-select
            @change="changeIssuer(form.issuerId)"
            v-model="form.issuerId"
            placeholder="请选择发证方"
            style="width: 100%"
          >
            <el-option
              v-for="item of enumList.lussuerList"
              :label="item.name"
              :value="item.id"
              :key="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="CPT：" prop="cptId">
          <el-select
            @change="changeCpt(form.cptId)"
            v-model="form.cptId"
            placeholder="请选择CPT"
            style="width: 100%"
          >
            <el-option
              v-for="item of enumList.cptList"
              :label="item.title"
              :value="item.id"
              :key="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="凭证属性：" prop="credentialSubject.claims">
          <el-row
            v-for="(item, index) in form.credentialSubject?.claims"
            :key="index + 1"
            style="width: 100%"
          >
            <el-col :span="24">
              <el-form-item
                :label="item.property"
                :prop="'credentialSubject.claims.' + index + '.value'"
                :rules="{
                  required: true,
                  message: '请输入属性',
                  trigger: 'blur',
                }"
              >
                <el-input
                  v-if="item.type === '00'"
                  type="text"
                  v-model.trim="item.value"
                  placeholder="请输入属性"
                >
                </el-input>
                <el-input
                  v-if="item.type === '01'"
                  type="number"
                  placeholder="请输入属性"
                  v-model.trim="item.value"
                >
                </el-input>
                <el-select
                style="width: 100%"
                  v-if="item.type === '02'"
                  v-model="item.value"
                  clearable
                  placeholder="请选择属性"
                >
                  <el-option label="true" :value="true" />
                  <el-option label="false" :value="false" />
                </el-select>
                <el-date-picker
                style="width: 100%"
                  v-if="item.type === '03'"
                  v-model="item.value"
                  type="datetime"
                  placeholder="请选择时间"
                />
              </el-form-item>
            </el-col>
          </el-row>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSubmit" :loading="isLoading"
            >确定</el-button
          >
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>
      <script setup>
import { reactive, ref, onMounted, toRefs } from "vue";
import { useRouter, useRoute } from "vue-router";
import { VoucherCreateApi, VoucherAttributeApi } from "@/api/Voucher";
import { DIDListEnumApi } from "@/api/DID";
import { ListCPTEnum } from "@/api/CPT";
import { lssuerListEnumApi } from "@/api/RegisterCenter";
import { ElMessage } from "element-plus";
import { dateToStringBySecond } from "@/utils/tool";
const router = useRouter();
const route = useRoute();
const backList = () => {
  router.go(-1);
};
const isLoading = ref(false);
const VoucherFormRef = ref("form");
let enumList = reactive({
  didList: [],
  lussuerList: [],
  cptList: [],
});
const form = reactive({
  issuerId: "",
  cptId: "",
  cptType: "00",
  credentialSubject: {
    claims: [
      {
        value: "",
      },
    ],
    id: "",
  },
});
/** 表单校验规则 */
const VoucherFormRefRules = {
  issuerId: [
    {
      required: true,
      message: "请选择发证方",
      trigger: "blur",
    },
  ],
  cptId: [
    {
      required: true,
      message: "请选择CPT",
      trigger: "blur",
    },
  ],
  credentialSubject: {
    id: [{ required: true, message: "请选择DID", trigger: "blur" }],
  },
};
const onSubmit = () => {
  VoucherFormRef.value?.validate(async (valid) => {
    if (valid) {
      isLoading.value = true;
      let attributeObj = {};
      form.credentialSubject.claims.forEach((item) => {
        if (item.type === "03") {
          item.value = dateToStringBySecond(item.value);
        }
        if (item.type === "01") {
          item.value = Number(item.value);
        }
        attributeObj[item.property] = item.value;
      });
      attributeObj = JSON.stringify(attributeObj);
      let parmars = {
        issuerId:form.issuerId,
        cptId: form.cptId,
        cptType: "00",
        credentialSubject: {
          id: form.credentialSubject?.id,
        },
      };
      parmars["credentialSubject"]["claims"] = attributeObj;
      VoucherCreateApi(parmars)
        .then((res) => {
          if (res.success) {
            ElMessage.success(res.msg || "凭证申请成功!");
            router.push({ path: "/VoucherManagement/VoucherList" });
            isLoading.value = false;
          } else {
            ElMessage.error(res.msg || "凭证申请失败!");
            isLoading.value = false;
          }
        })
        .catch((err) => {
          ElMessage.error(err.msg || "凭证申请失败!");
          isLoading.value = false;
        });
    } else {
      return false;
    }
  });
};
//did列表
const didListEnum = () => {
  DIDListEnumApi()
    .then((res) => {
      if (res.success) {
        enumList.didList = res.data.list || [];
      } else {
        ElMessage.error(res.msg);
      }
    })
    .catch((err) => {
      ElMessage.error(err.msg);
    });
};
//发证方列表
const lssuerListEnum = () => {
  lssuerListEnumApi()
    .then((res) => {
      if (res.success) {
        enumList.lussuerList = res.data.content || [];
      } else {
        ElMessage.error(res.msg);
      }
    })
    .catch((err) => {
      ElMessage.error(err.msg);
    });
};
//cpt列表
const cptListEnum = (lssuerId) => {
  let parmars = {
    publisherId: lssuerId,
  };
  ListCPTEnum(parmars)
    .then((res) => {
      if (res.success) {
        enumList.cptList = res.data.cptTitleInfo || [];
      } else {
        ElMessage.error(res.msg);
        enumList.cptList = [];
      }
    })
    .catch((err) => {
      ElMessage.error(err.msg);
      enumList.cptList = [];
    });
};
// 凭证属性
const attributeList = (cptId) => {
  let parmars = {
    cptId,
  };
  VoucherAttributeApi(parmars)
    .then((res) => {
      if (res.success) {
        form.credentialSubject.claims = res.data.properties || [];
      } else {
        ElMessage.error(res.msg);
        form.credentialSubject.claims = [];
      }
    })
    .catch((err) => {
      ElMessage.error(err.msg);
      form.credentialSubject.claims = [];
    });
};
//发证方改变时
const changeIssuer = (lssuerId) => {
  form.cptId = "";
  form.credentialSubject.claims = [];
  cptListEnum(lssuerId);
};
//cpt改变时
const changeCpt = (cptId) => {
  form.credentialSubject.claims = [];
  attributeList(cptId);
};
onMounted(() => {
  didListEnum();
  lssuerListEnum();
});
</script>
      
      <style lang="scss" scoped>
@import "../../assets/css/cindex.scss";
:deep .el-upload-list--picture-card .el-upload-list__item-actions span + span {
  margin-left: 0.5em;
}
.add_icon {
  font-size: 18px;
  // margin-left: 10px;
  cursor: pointer;
  height: 100%;
}
.delete_icon {
  font-size: 18px;
  margin-left: 10px;
  cursor: pointer;
  height: 100%;
}
.box {
  display: flex;
  .left_txContent {
    // width: 200px;
    width: 50%;
    padding: 20px;
    // height: 200px;
    // background: pink;
    border-radius: 8px;
    border: 1px solid #d6e9c6;
    word-break: break-word;
    margin-right: 10px;
  }
  .rigth_txContent {
    width: 50%;
    padding: 20px;

    // width: 200px;
    // height: 200px;
    // background: red;
    border-radius: 8px;
    border: 1px solid #d6e9c6;
    word-break: break-word;
  }
}
.file_cn {
  // width: 130px;
  width: 100%;
  font-size: 14px;
  //   text-align: right;
  // margin-left: 70px;
  margin-bottom: 5px;
  padding: 10px;
  border-radius: 4px;
  border: 1px dashed #a6a6a6;
  div {
    margin-bottom: 15px;
  }
}
.Sign_content {
  display: flex;
  font-size: 14px;
  .Sign_content_key {
    display: inline-flex;
    justify-content: flex-end;
    align-items: flex-start;
    flex: 0 0 auto;
    height: 32px;
    line-height: 32px;
    padding: 0 12px 0 0;
    box-sizing: border-box;
    width: 90px;
  }
  .Sign_content_data {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    flex: 1;
    line-height: 32px;
    position: relative;
    min-width: 0;
    text-overflow: ellipsis;
    overflow: hidden;
    white-space: nowrap;
  }
}

.red {
  color: red;
}
.green {
  color: green;
}
.el-form-item .el-form-item {
  width: 100%;
  margin-bottom: 20px;
}
.el-form-item .el-row {
  border: 1px soild #dcdfe6 !important;
  //   box-sizing: unset;
}
.center {
  display: flex;
  justify-content: start;
}
.wordWrap {
  word-break: break-word;
}
:deep .el-descriptions .el-descriptions__label {
  display: inline-block;
  width: 100px;
  text-align: right;
}
.add_icon {
  font-size: 18px;
  margin-left: 10px;
  cursor: pointer;
  height: 100%;
}
.delete_icon {
  font-size: 18px;
  margin-left: 10px;
  cursor: pointer;
  height: 100%;
}
.center {
  display: flex;
  justify-content: start;
}
</style>